package com.itqf.controller;

import com.itqf.entity.Cart;
import com.itqf.entity.User;
import com.itqf.service.CartService;
import com.itqf.service.impl.CartServiceImpl;
import com.itqf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "CartController", value = "/cart")
public class CartController extends BaseServlet {

    //初始化业务逻辑对象
    private static CartService cartService = new CartServiceImpl();

    public String create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.判断是否已经登录
        User user = (User) request.getSession().getAttribute("loginUser");
        //1.1尚未登录
        if (user == null) {
            //当前用户没有登录
            //存储提示信息，返回给前端页面显示
            request.getSession().setAttribute("msg", "添加购物车必须先登录！！！");
            //重定向至登录页面
            return Constants.REDIRECT + "/login.jsp";
        }

        //1.2已经登录
        //2.商品的id和用户的id
        //获取用户的ID
        int uid = user.getUid();
        //获取商品id
        String pid = request.getParameter("pid");

        //调用业务逻辑
        cartService.createCart(uid, pid);

        return Constants.FORWARD + "/cartSuccess.jsp";
    }

    public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        //1.判断是否已经登录
        User user = (User) request.getSession().getAttribute("loginUser");
        //1.1尚未登录
        if (user == null) {
            //当前用户没有登录
            //存储提示信息，返回给前端页面显示
            request.getSession().setAttribute("msg", "添加购物车必须先登录！！！");
            //重定向至登录页面
            return Constants.REDIRECT + "/login.jsp";
        }

        //2.获取参数
        int uid = user.getUid();

        //3.调用业务逻辑，进行数据查询
        List<Cart> list = cartService.findAll(uid);//根据用户名查询，该用户的所有购物车数据

        request.setAttribute("list", list);
        return Constants.FORWARD + "/cart.jsp";
    }

    public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取cid
        String cid = request.getParameter("cid");

        //2.调用业务逻辑，进行删除
        cartService.deleteCartByCid(cid);

        //3.转发到展示
        return Constants.FORWARD + "/cart?method=show";
    }

    public String update(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取cid
        String cid = request.getParameter("cid");
        String price = request.getParameter("price");//商品的单价
        String cnum = request.getParameter("cnum");//修改后的数量


        //2.调用业务逻辑，进行删除
        cartService.updateCartByCid(cid, price, cnum);

        //3.刷新展示页面
        return Constants.FORWARD + "/cart?method=show";
    }

    public String clear(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取uid
        String uid = request.getParameter("uid");
        String price = request.getParameter("price");//商品的单价
        String cnum = request.getParameter("cnum");//修改后的数量


        //2.调用业务逻辑，进行删除
        cartService.clearCart(uid);

        //3.刷新展示页面
        return Constants.FORWARD + "/cart?method=show";
    }
}
